package com.haidong.component;

import com.haidong.SqlFunction;
import com.haidong.pojo.House;
import com.haidong.pojo.Worker;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.time.LocalDate;
import java.util.List;
/**
 * 主要是工人界面的增删查改
 */
public class MyWorkerPanel extends JPanel {
    public static JPanel select(){
        JPanel panel = new JPanel(new BorderLayout());

        JPanel up = new JPanel(new GridLayout(1,11));
        JLabel l1 = new JLabel("姓名:");
        JLabel l2 = new JLabel("性别:");
        JLabel l3 = new JLabel("行政职务:");
        JLabel l4 = new JLabel("具体职务:");
        JLabel l5 = new JLabel("配偶:");
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        TextField tf3 = new TextField();
        TextField tf4 = new TextField();
        TextField tf5 = new TextField();
        JButton bt1 = new JButton("查询");

        up.add(l1);
        up.add(tf1);
        up.add(l2);
        up.add(tf2);
        up.add(l3);
        up.add(tf3);
        up.add(l4);
        up.add(tf4);
        up.add(l5);
        up.add(tf5);
        up.add(bt1);

        JPanel center = new JPanel(new GridLayout(1,1));

        //动态显示表格内容,用defaultTable是为了保证数据刷新,用scrollPane是为了显示表头
        Object[] workerHead = new Object[]{"id:","姓名*:","性别:","部门:","政治职务:","具体职务*:","入职时间:","最高行政职务时间:","最高具体职务时间*:","配偶姓名:","房子:","房产证id:","档案id:","房主:"};
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},workerHead);
        JTable table = new JTable(tableModel);
        table.setRowHeight(30);
        JScrollPane scrollPane = new JScrollPane(table);
        center.add(scrollPane);

        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Worker worker = new Worker();
                worker.setName("".equals(tf1.getText()) ? null :"%"+tf1.getText()+"%");
                worker.setGender("".equals(tf2.getText()) ? null :tf2.getText().charAt(0));
                worker.setPoliJob("".equals(tf3.getText()) ? null :"%"+tf3.getText()+"%");
                worker.setDetailJob("".equals(tf4.getText()) ? null :"%"+tf4.getText()+"%");
                worker.setCoupleName("".equals(tf5.getText()) ? null :"%"+tf5.getText()+"%");
                List<Worker> workers = SqlFunction.selectWorker(worker);
                Object[][] data = new Object[workers.size()][14];
                for (int i = 0; i < workers.size(); i++) {
                    data[i] = getSingleWorkerData(workers.get(i));
                }
                tableModel.setDataVector(data,workerHead);
            }
        });
        panel.add(up,BorderLayout.NORTH);
        panel.add(center,BorderLayout.CENTER);

        return panel;
    }
    public static Object[] getSingleWorkerData(Worker worker){
        String none = "暂无";
        String isHost = "未知";
        if(worker.getHost()) isHost = "是";
        else if(worker.getHost()==false) isHost = "否";
        //接受数据并过滤掉null
        return new Object[]{
                worker.getId()!= null ? worker.getId() : none,
                worker.getName()!= null ? worker.getName() : none,
                worker.getGender()!= null ? worker.getGender() : none,
                worker.getDeptId()!= null ? SqlFunction.idToDepart(worker.getDeptId()) : none,
                worker.getPoliJob()!= null ? worker.getPoliJob() : none,
                worker.getDetailJob()!= null ? worker.getDetailJob() : none,
                worker.getJoinTime()!= null ? worker.getJoinTime() : none,
                worker.getUpperPoliTime()!= null ? worker.getUpperPoliTime() : none,
                worker.getUpperDetailTime()!= null ? worker.getUpperDetailTime() : none,
                worker.getCoupleName()!= null ? worker.getCoupleName() : none,
                worker.getHouseId()!= null ? SqlFunction.idToHouseName(worker.getHouseId()) : none,
                worker.getHouseProveId()!= null ? worker.getHouseProveId() : none,
                worker.getFileId()!= null ? worker.getFileId() : none,
                isHost,
                };
    }

    public static JPanel add(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,3));
        JLabel[] labels = {new JLabel("姓名*:"), new JLabel("性别:"),
                new JLabel("部门:"), new JLabel("行政职务:"),
                new JLabel("具体职务*:"), new JLabel("入职时间:"),
                new JLabel("最高行政职务时间:"), new JLabel("最高具体职务时间*:"),
                new JLabel("配偶姓名:"), new JLabel("房子:"),
                new JLabel("房产证id:"), new JLabel("档案id:"),
                new JLabel("是否房主:")};
        TextField[] textField = new TextField[13];
        JPanel[] panels = new JPanel[13];
        for (int i = 0; i < 13; i++) {
            textField[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            labels[i].setFont(new Font("Dialog",0,20));
            textField[i].setFont(new Font("Dialog",0,25));
            panels[i].add(labels[i]);
            panels[i].add(textField[i]);
        }

        JButton button = new JButton("确认添加");
        button.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("".equals(textField[0].getText())||"".equals(textField[4].getText())||"".equals(textField[7].getText())){
                    label.setText("请输入必选项!");
                }else{
                    Worker worker = new Worker();
                    worker.setName("".equals(textField[0].getText()) ? null :textField[0].getText());
                    worker.setGender("".equals(textField[1].getText()) ? null :textField[1].getText().charAt(0));
                    worker.setDeptId("".equals(textField[2].getText()) ? null :Integer.valueOf(textField[2].getText()));
                    worker.setPoliJob("".equals(textField[3].getText()) ? null :textField[3].getText());
                    worker.setDetailJob("".equals(textField[4].getText()) ? null :textField[4].getText());
                    worker.setJoinTime("".equals(textField[5].getText()) ? null :LocalDate.parse(textField[5].getText()));
                    worker.setUpperPoliTime("".equals(textField[6].getText()) ? null :LocalDate.parse(textField[6].getText()));
                    worker.setUpperDetailTime("".equals(textField[7].getText()) ? null :LocalDate.parse(textField[7].getText()));
                    worker.setCoupleName("".equals(textField[8].getText()) ? null :textField[8].getText());
                    worker.setHouseId("".equals(textField[9].getText()) ? null :Integer.valueOf(textField[9].getText()));
                    worker.setHouseProveId("".equals(textField[10].getText()) ? null :textField[10].getText());
                    worker.setFileId("".equals(textField[11].getText()) ? null :Integer.valueOf(textField[11].getText()));
                    Integer isHost = "".equals(textField[12].getText()) ? null :Integer.valueOf(textField[12].getText());
                    if(isHost==null) worker.setHost(null);
                    else if(isHost==0) worker.setHost(false);
                    else worker.setHost(true);

                    SqlFunction.insertWorker(worker);
                    label.setText("添加成功!");
                }

            }
        });


        for (int i = 0; i < 3; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 3; i < 6; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 6; i < 9; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 9; i < 12; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(panels[12]);
        upper.add(new JLabel(""));
        upper.add(button);
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}

        panel.add(upper);
        return panel;
    }

    public static JPanel del(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,1));
        JLabel label1 = new JLabel("请输入员工id:");
        label1.setFont(new Font("Dialog",0,20));
        TextField textField1 = new TextField();
        textField1.setFont(new Font("Dialog",0,25));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        panel1.add(label1);
        panel1.add(textField1);
        JButton button1 = new JButton("确认删除");
        button1.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SqlFunction.deleteWorker(Integer.valueOf(textField1.getText()));
                label.setText("删除成功!");
            }
        });
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(button1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        panel.add(new JLabel(""));
        panel.add(upper);
        panel.add(new JLabel(""));
        return panel;
    }

    public static JPanel update(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,3));
        JLabel[] labels = {new JLabel("姓名:"), new JLabel("性别:"),
                new JLabel("部门:"), new JLabel("行政职务:"),
                new JLabel("具体职务:"), new JLabel("入职时间:"),
                new JLabel("最高行政职务时间:"), new JLabel("最高具体职务时间:"),
                new JLabel("配偶姓名:"), new JLabel("房子:"),
                new JLabel("房产证id:"), new JLabel("档案id:"),
                new JLabel("是否房主:"),new JLabel("请输入id(此为索引):")};
        TextField[] textField = new TextField[14];
        JPanel[] panels = new JPanel[14];
        for (int i = 0; i < 14; i++) {
            textField[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            labels[i].setFont(new Font("Dialog",0,20));
            textField[i].setFont(new Font("Dialog",0,25));
            panels[i].add(labels[i]);
            panels[i].add(textField[i]);
        }

        JButton button = new JButton("确认修改");
        button.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Worker worker = new Worker();
                worker.setName("".equals(textField[0].getText()) ? null :textField[0].getText());
                worker.setGender("".equals(textField[1].getText()) ? null :textField[1].getText().charAt(0));
                worker.setDeptId("".equals(textField[2].getText()) ? null :Integer.valueOf(textField[2].getText()));
                worker.setPoliJob("".equals(textField[3].getText()) ? null :textField[3].getText());
                worker.setDetailJob("".equals(textField[4].getText()) ? null :textField[4].getText());
                worker.setJoinTime("".equals(textField[5].getText()) ? null :LocalDate.parse(textField[5].getText()));
                worker.setUpperPoliTime("".equals(textField[6].getText()) ? null :LocalDate.parse(textField[6].getText()));
                worker.setUpperDetailTime("".equals(textField[7].getText()) ? null :LocalDate.parse(textField[7].getText()));
                worker.setCoupleName("".equals(textField[8].getText()) ? null :textField[8].getText());
                worker.setHouseId("".equals(textField[9].getText()) ? null :Integer.valueOf(textField[9].getText()));
                worker.setHouseProveId("".equals(textField[10].getText()) ? null :textField[10].getText());
                worker.setFileId("".equals(textField[11].getText()) ? null :Integer.valueOf(textField[11].getText()));
                Integer isHost = "".equals(textField[12].getText()) ? null :Integer.valueOf(textField[12].getText());
                if(isHost==null) worker.setHost(null);
                else if(isHost==0) worker.setHost(false);
                else worker.setHost(true);
                worker.setId("".equals(textField[13].getText()) ? null :Integer.valueOf(textField[13].getText()));

                SqlFunction.updateWorker(worker);
                label.setText("修改成功!");
            }
        });


        for (int i = 0; i < 3; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 3; i < 6; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 6; i < 9; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 9; i < 12; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(panels[12]);
        upper.add(panels[13]);
        upper.add(button);
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}

        panel.add(upper);
        return panel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = MyWorkerPanel.select();
        frame.add(panel);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}
